<?php
/**
 * Report.class.php
 * 报表工具
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-02-24
 */
namespace Admin\Util;

class Report {

	/**
	 * 单例模式声明
	 */
	static public $self;
	static public function getInstance(){
		if(!self::$self){
			self::$self 	= new self();
		}

		return self::$self;
	}

	/**
	 * 购物车报表数据获取
	 * @param   $timeS 起始时间
	 * @param   $timeE 结束时间
	 * @return  $list [<description>]
	 */
	function getShopCart($timeS, $timeE){
		$list 	= M('t_report_shopcart', null)
			->where('time >= %d and time <= %d', $timeS, $timeE)
			->order('time')
			->cache(true)
			->select();

		foreach($list as &$item){
			$item['date']	= date('Y-m-d', $item['time']);
		}
		return $list;
	}

	/**
	 * 获取收入报表统计
	 * @param   $timeS 开始时间
	 * @param   $timeE 结束时间
	 * @return  $list
	 */
	function getIncome($timeS, $timeE){
		$list 	= M('t_report_income', null)
			->where('time >= %d and time <=%d', $timeS, $timeE)
			->order('time')
			->cache(true)
			->select();
		foreach($list as &$item){
			$item['date']	= date('Y-m-d', $item['time']);
		}
		return $list;
	}

	/**
	 * 获取课程报表统计
	 * @param   $timeS 开始时间
	 * @param   $timeE 结束时间
	 * @return  $reportInfo 报表信息
	 */
	function getCourse($timeS, $timeE){
		// $list 	= M('t_report_course', null)
		// 	->where('time >= %d and time <= %d', $timeS, $timeE)
		// 	->field('time, count, amount')
		// 	->cache(true)
		// 	->order('time')
		// 	->select();
		// foreach($list as &$item){
		// 	$item['date']	= date('Y-m-d', $item['time']);
		// }
		// 
		$list 	= M('t_report_course a', null)
			->join('left join t_course b on a.course_id = b.id')
			->where('time >= %d and time <= %d', $timeS, $timeE)
			->order('time')
			->field('course_id, time, name, count, amount')
			->select();

		// dump($list);
		foreach($list as &$item){
			$item['date']	= date('Y-m-d', $item['time']);
			if($item['course_id'] != 0 && !$course[$item['course_id']])
				$course[$item['course_id']] 	= $item['name'];
			$data[$item['date']][$item['course_id']]	= $item;

			if(!in_array($item['date'], $xAxis))
				$xAxis[] 	= $item['date'];
		}
		foreach($course as $cId => $cName){
			$_name 	= $cName;
			$_data 	= array();
			foreach($xAxis as $date){
				$_data['count'][] 	= $data[$date][$cId]['count'] ? $data[$date][$cId]['count'] : 0;
				$_data['amount'][] 	= $data[$date][$cId]['amount'] ? $data[$date][$cId]['amount'] : 0;
			}
			$series['count'][] 	= array('name' => $_name, 'data' => $_data['count']);
			$series['amount'][] 	= array('name' => $_name, 'data' => $_data['amount']);
		}
		if(!$series){
			foreach($xAxis as $val){
				$series['count'][] 	= 0;
				$series['amount'][]	= 0;
			}
		}
		// dump($xAxis);
		// dump($series);
		return array('x_axis' => $xAxis, 'series' => $series);
	}
}